package com.fsquare.shipping.api.wrapper;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import org.broadleafcommerce.core.order.fulfillment.domain.BandedPriceFulfillmentOption;
import org.broadleafcommerce.core.order.fulfillment.domain.FulfillmentPriceBand;
import org.broadleafcommerce.core.web.api.wrapper.BroadleafEnumerationTypeWrapper;
import org.springframework.context.ApplicationContext;

import com.fsquare.shipping.service.ExtFulfillmentOptionService;

@XmlRootElement(name = "bandedPriceFulfillmentOptionWrapper")
@XmlAccessorType(value = XmlAccessType.FIELD)
public class BandedPriceFulfillmentOptionWrapper extends ExtFulfillmentOptionWrapper{
	
    protected List<FulfillmentPriceBandWrapper> bands = new ArrayList<FulfillmentPriceBandWrapper>();
    
	public void wrapDetails(BandedPriceFulfillmentOption model, HttpServletRequest request) {
		super.wrapDetails(model, request);
//		model = (BandedPriceFulfillmentOption)model;
		this.id = model.getId();
        if (model.getBands() != null && !model.getBands().isEmpty()) {
        	for(FulfillmentPriceBand fulfillmentPriceBand : model.getBands()){
        		FulfillmentPriceBandWrapper band = (FulfillmentPriceBandWrapper) context.getBean(FulfillmentPriceBandWrapper.class.getName());
        		band.wrapDetails(fulfillmentPriceBand, request);
        		bands.add(band);
        	}
        	
            this.fulfillmentType = (BroadleafEnumerationTypeWrapper) context.getBean(BroadleafEnumerationTypeWrapper.class.getName());
            this.fulfillmentType.wrapDetails(model.getFulfillmentType(), request);
        }
	}

	public void wrapSummary(BandedPriceFulfillmentOption model, HttpServletRequest request) {
		wrapDetails(model, request);
	}

	@Override
	public BandedPriceFulfillmentOption unwrap(HttpServletRequest request, ApplicationContext context) {
		super.unwrap(request, context);
		ExtFulfillmentOptionService extFulfillmentOptionService = (ExtFulfillmentOptionService) context.getBean("extFulfillmentOptionService");
		BandedPriceFulfillmentOption bandedPriceFulfillmentOption = extFulfillmentOptionService.createBandedPriceFulfillmentOption();
		
		return bandedPriceFulfillmentOption;
	}
	
}
